package com.openlogic.common.logic.func.aviator;

import java.util.Map;

import org.apache.commons.math3.stat.StatUtils;

import com.googlecode.aviator.runtime.function.AbstractVariadicFunction;
import com.googlecode.aviator.runtime.type.AviatorObject;
import com.googlecode.aviator.runtime.type.AviatorRuntimeJavaType;

/**
 * @ClassName: PopulationVarianceFunc
 * @Description: 总体方差自定义函数 标准差σ=sqrt(s^2)
 * @author weiyongpeng
 * @date 2024年12月27日 上午9:27:28
 */
public class PopulationVarianceFunc extends AbstractVariadicFunction {

	private static final long serialVersionUID = -7377110880312266008L;

	@Override
	public String getName() {
		return "cus_statis.getPopulationVariance";
	}

	@Override
	public AviatorObject variadicCall(Map<String, Object> env, AviatorObject... args) {
		double[] x = new double[args.length];
		for (int i = 0; i < args.length; i++) {
			Object value = args[i].getValue(env);
			if (value instanceof Long) {
				x[i] = (Double.parseDouble(String.valueOf((Long) value)));
			} else if (value instanceof Double) {
				x[i] = Double.parseDouble(String.valueOf(value));
			} else if (value instanceof Integer) {
				x[i] = Double.parseDouble(String.valueOf((Integer) value));
			} else {
				x[i] = Double.parseDouble(String.valueOf(value));
			}
		}
		return AviatorRuntimeJavaType.valueOf(Math.round(StatUtils.populationVariance(x) * 10000.0) / 10000.0);
	}

}
